﻿using System;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;

namespace HSIA
{
    public partial class FrmNetSet : Form
    {
        DatabaseObject dbo;
        string sSMSCOM = "";
        string sValidateType = "";
        string sListenIP = "";
        int iFrequency = 30;
        int iPort = 4001;
        string sCenterNumber = "";

        public FrmNetSet(DatabaseObject vdbo)
        {
            dbo = vdbo;
            InitializeComponent();
        }

        private void FrmParamSet_Load(object sender, EventArgs e)
        {
            sSMSCOM = AmateBussiness.GetSystemParamValue(dbo, "SMSPort", "COM2").ToString();
            sValidateType = AmateBussiness.GetSystemParamValue(dbo, "ValidateType", dbo.GetLanguage(184)).ToString();//"无"
            sListenIP = AmateBussiness.GetSystemParamValue(dbo, "ListenIP ", "").ToString();
            iPort = Convert.ToInt32(AmateBussiness.GetSystemParamValue(dbo, "NetPort", 4001));
            sCenterNumber = AmateBussiness.GetSystemParamValue(dbo, "CenterNumber", "13010710500").ToString();
            iFrequency = 30;
            if (!int.TryParse(AmateBussiness.GetSystemParamValue(dbo, "GatherFrequency", 30).ToString(), out iFrequency))
            {
                AmateBussiness.SetSystemParamValue(dbo, "GatherFrequency", "30");
            }
            if (PublicFunc.iNetConnect == 0)
            {
                IPHostEntry ihe = Dns.GetHostByName(Dns.GetHostName());
                cbIP.DataSource = ihe.AddressList;
            }
            else
            {
                cbIP.Items.Add(sListenIP);
            }
            cbSMSCOM.Text = sSMSCOM;
            txtPort.Text = iPort.ToString();
            txtFrequency.Text = iFrequency.ToString();
            cbIP.Text = sListenIP;
            tbCenterNumber.Text = sCenterNumber;

            groupBox6.Text = dbo.GetLanguage(504);

            btnOK.Text = dbo.GetLanguage(363); //193确定
            btnCancel.Text = dbo.GetLanguage(51);//51取消
            this.Text = dbo.GetLanguage(281);//51取消

            foreach (Control control in groupBox6.Controls)
            {
                Label lbl = control as Label;
                if (lbl == null || lbl.Tag == null || lbl.Tag.ToString() == "") continue;
                lbl.Text = dbo.GetLanguage(lbl.Tag);
            }
            foreach (Control control in this.Controls)
            {
                Label lbl = control as Label;
                if (lbl == null || lbl.Tag == null || lbl.Tag.ToString() == "") continue;
                lbl.Text = dbo.GetLanguage(lbl.Tag);
            }
            //chkShowLog.Checked = (sShowLog == "1");
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string sSQL = "";
            int iNewFrequency = iFrequency;
            if (!int.TryParse(txtFrequency.Text, out iNewFrequency))
            {
                MessageBox.Show(dbo.GetLanguage(506));//"采集周期应为整数，请重新输入！"
                txtFrequency.Focus();
                return;
            }
            //if (cbCOM.Text == cbSMSCOM.Text)
            //{
            //    MessageBox.Show(dbo.GetLanguage(507));//"COM端口与短信端口选择一样，请重新选择！"
            //    cbCOM.Focus();
            //    return;
            //}
            if (tbCenterNumber.Text.ToString().Trim() != "")
            {
                //if (tbCenterNumber.Text.ToString().Trim().Length == 11)
                //{
                //    string s = tbCenterNumber.Text.ToString().Trim();
                //    string sleft = s.Substring(0, 5);
                //    string sright = s.Substring(5, 6);
                //    try
                //    {
                //        int i = Convert.ToInt32(sleft);
                //        int j = Convert.ToInt32(sright);
                //    }
                //    catch
                //    {
                //        MessageBox.Show(dbo.GetLanguage(508));//"短息中心号码输入有误，请输入！"
                //        tbCenterNumber.Focus();
                //        return ;
                //    }
                //    sSQL = "update Sys_Param set Value='" + tbCenterNumber.Text + "' where Name='CenterNumber';";
                //}
                //else
                //{
                //    MessageBox.Show(dbo.GetLanguage(508));//"短息中心号码输入有误，请输入！");
                //    tbCenterNumber.Focus();
                //    return ;
                //}
                sSQL = "update Sys_Param set Value='" + tbCenterNumber.Text + "' where Name='CenterNumber';";

            }
            else
            {
                MessageBox.Show(dbo.GetLanguage(509));//"请输入短息中心号码！");
                tbCenterNumber.Focus();
                return;
            }

            //if (cbCOM.Text != sCOM)
            //{
            //    sSQL = sSQL + "update Sys_Param set Value='" + cbCOM.Text + "' where Name='COMPort';";
            //    //sCOM = cbCOM.Text;
            //}
            //if (cbSMSCOM.Text != sSMSCOM)
            //{
            //    sSQL = sSQL + "update Sys_Param set Value='" + cbSMSCOM.Text + "' where Name='SMSPort';";
            //    //sCOM = cbCOM.Text;
            //}
            //if (cbBaudRate.Text != sBaudRate)
            //{
            //    sSQL = sSQL + "update Sys_Param set Value='" + cbBaudRate.Text + "' where Name='BaudRate';";
            //    //sBaudRate = cbBaudRate.Text;
            //}
            //if (cbDataBits.Text != sDataBits)
            //{
            //    sSQL = sSQL + "update Sys_Param set Value='" + cbDataBits.Text + "' where Name='DataBits';";
            //    //sDataBits = cbDataBits.Text;
            //}
            //if (cbStopBits.Text != sStopBits)
            //{
            //    sSQL = sSQL + "update Sys_Param set Value='" + cbStopBits.Text + "' where Name='StopBits';";
            //    //sStopBits = cbStopBits.Text;
            //}
            //if (cbValidateType.Text != sValidateType)
            //{
            //    sSQL = sSQL + "update Sys_Param set Value='" + cbValidateType.Text + "' where Name='ValidateType';";
            //    //sValidateType = cbValidateType.Text;
            //}
            if (iNewFrequency != iFrequency)
            {
                sSQL = sSQL + "update Sys_Param set Value='" + iNewFrequency + "' where Name='GatherFrequency';";
                //sValidateType = cbValidateType.Text;
            }
            if (cbIP.Text != sListenIP)
            {
                sSQL = sSQL + "update Sys_Param set Value='" + cbIP.Text + "' where Name='ListenIP';";
                //sValidateType = cbValidateType.Text;
            }
            if (txtPort.Text != iPort.ToString())
            {
                if (!Int32.TryParse(txtPort.Text, out iPort))
                {
                    MessageBox.Show(dbo.GetLanguage(212));//"请输入正确的端口号！"
                    txtPort.Focus();
                    return;
                }
                sSQL = sSQL + "update Sys_Param set Value='" + txtPort.Text + "' where Name='NetPort';";
                iPort = Convert.ToInt32(txtPort.Text);
            }
            //if (chkShowLog.Checked != (sShowLog == "1"))
            //{
            //    if (chkShowLog.Checked) sShowLog = "1"; else sShowLog = "0";
            //    sSQL = sSQL + "update Sys_Param set Value='" + sShowLog + "' where Name='ShowLog';";
            //}
            if (sSQL != "")
            {
                string sRet = dbo.ExcuteSQL(sSQL);
                if (sRet == "OK")
                {
                    sRet = dbo.GetLanguage(201);// "参数修改成功！程序需重新启动后才能生效。";
                    dbo.RefreshSystemParam();

                    AmateBussiness.SendMasgeToServer(dbo, PC.WM_ParameterSet, 100);
                    MessageBox.Show(sRet);
                }
                else
                {
                    MessageBox.Show(sRet);
                    return;
                }
                //Close();
            }
            this.DialogResult = DialogResult.OK;
        }

    }
}
